package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-16
 * Time: 下午3:47
 * To change this template use File | Settings | File Templates.
 */
public class FieldSearchResultAnalysis {

    private static Logger logger = LoggerFactory.getLogger(FieldSearchResultAnalysis.class);

    public static void main(String[] args) {
        String classPath = FileUtils.class.getResource("/").getPath();
        String beforeFile = classPath + "dic/before_field_search.log";
        String afterFile = classPath + "dic/after_field_search.log";
        List<String> beforeList = FileUtils.readFileAsString(beforeFile, "UTF-8");
        List<String> afterList = FileUtils.readFileAsString(afterFile, "UTF-8");
        long startTime = System.currentTimeMillis();
        System.out.println("test start...");
        StringBuilder doctorStr = new StringBuilder();
        List<Keyword> diffKeyword = new ArrayList<Keyword>();
        String tabString = "\t";
        int count = 0;
        for (int i = 0; i < beforeList.size(); i++) {
            String[] beforeStringSplit = beforeList.get(i).split(tabString);
            String[] afterStringSplit = afterList.get(i).split(tabString);
            if (beforeStringSplit.length == 4) {
                if (!beforeStringSplit[1].equals(afterStringSplit[1])) {
                    diffKeyword.add(new Keyword(beforeStringSplit[0],
                            Integer.valueOf(beforeStringSplit[1]), Integer.valueOf(afterStringSplit[1])));
                }
            }
        }
        Collections.sort(diffKeyword);

        for(Keyword keyword : diffKeyword){
            doctorStr.append(keyword.word).append(tabString);
            doctorStr.append(keyword.beforeCount).append(tabString);
            doctorStr.append(keyword.afterCount).append(tabString);
            doctorStr.append(keyword.difference).append(tabString);
            doctorStr.append(keyword.diffRate).append("\n");
        }

        System.out.println(count);
        logger.info(doctorStr.toString());
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime / 1000) + " seconds.");
    }

    private static class Keyword implements Comparable {
        private String word;
        private Integer beforeCount;
        private Integer afterCount;
        private Integer difference;
        private Float diffRate;

        public Keyword(String word, Integer beforeCount, Integer afterCount) {
            this.word = word;
            this.beforeCount = beforeCount;
            this.afterCount = afterCount;
            difference = beforeCount - afterCount;
            diffRate = Float.valueOf(difference) / Float.valueOf(afterCount);
        }

        @Override
        public int compareTo(Object o) {
            Keyword newKeyword = (Keyword) o;
            if (this.diffRate > newKeyword.diffRate) {
                return 1;
            } else if (this.diffRate < newKeyword.diffRate) {
                return -1;
            } else {
                return 0;
            }
        }
    }

}
